// pages/worker/worker.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    websocket:"",
    uid:"",//联系(租客或购房客)
    id:"",//当前需求的数据id
    list:[],
		content:"",
		_this:this,
		username:"",
		userimage:"",
		leftusername:"",
		leftuserimage:"",
	},
	getLeftName(token){
		wx.request({
			url: 'http://www.house.com/index.php/chat/left_name',
			data:{uid:token},
			success:res=>{
				if(res.data.code==200){
					this.setData({
						leftusername:res.data.data.user_name
					})
				
				}
			}
		})
	},
	getLeftImage(token){
		wx.request({
			url: 'http://www.house.com/index.php/chat/left_image',
			data:{uid:token},
			success:res=>{
				if(res.data.code==200){
					this.setData({
						leftuserimage:res.data.data.user_image
					})
		
				}
			}
		})
	},
	getUserName(token){
		wx.request({
			url: 'http://www.house.com/index.php/chat/user_name',
			header:{token:token},
			success:res=>{
				if(res.data.code==200){
					this.setData({
						username:res.data.data.user_name
					})
				}
			}
		})
	},
	getUserImage(token){
		wx.request({
			url: 'http://www.house.com/index.php/chat/user_image',
			header:{token:token},
			success:res=>{
				if(res.data.code==200){
					this.setData({
						userimage:res.data.data.user_image
					})
					console.log(this.data.userimage)
				}
			}
		})
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
		var _this=this
    this.setData({
      uid:options.uid,
      id:options.id
		})
		this.getUserImage(wx.getStorageSync('token'));
		this.getUserName(wx.getStorageSync('token'));
		this.getLeftImage(options.uid);
		this.getLeftName(options.uid);
    var websocket=wx.connectSocket({
      url: 'ws://101.34.32.250:9502'
    })
    this.setData({
      websocket:websocket
    })
    
    wx.onSocketOpen(function(res) {
      console.log(res)
    })
    websocket.onMessage(function(res){
      var JsonType = JSON.parse(res.data);
      console.log(JsonType)
      switch(JsonType.type){
        case "init":
          var data={
            "type":"init",
            "token":wx.getStorageSync('token'),
            "to_id":options.uid
          }
          var strData =JSON.stringify(data);
          websocket.send({
            "data":strData,
          });
          break;
        case "text":
					var newSet =JsonType.data;
					var list =_this.data.list;
          var newList = list.concat(newSet)
          console.log(newList)
					_this.setData({
						list:newList
          })
          break;

      }
    });
  },
  send(){
    var uid = this.data.uid
    var content=this.data.content
    var websocket = this.data.websocket
    if(!content){
      wx.showToast({
        title: '内容不能为空',
      })
      return
    }
    var data={
      "type":"text",
      "uid":uid,
      "content":content,
      "token":wx.getStorageSync('token')
    }
    var strData = JSON.stringify(data)
    websocket.send({"data":strData});
  },
  content(e){
    this.setData({
      content:e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})